package org.wk.news.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.wk.core.common.view.ResponseVo;
import org.wk.news.service.NewsManageService;
import org.wk.news.service.NewsPraiseService;
import org.wk.news.view.NewsManageVo;

/**
 * @ClassName NewsController
 * @Description 资讯
 * @Author ZhaoPing'an
 * @Date 2018/6/8 15:45
 */
@Controller
public class NewsPraiseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private NewsPraiseService newsPraiseService;

    @RequestMapping(value="/newspraise",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String newspraise(@RequestBody JSONObject jsonObject){
        logger.info("新闻点赞>>>>{}",jsonObject.toJSONString());
        String userId = jsonObject.getString("userId");
        String newsId = jsonObject.getString("newsId");
        if (Strings.isNullOrEmpty(userId) || Strings.isNullOrEmpty(newsId)) {
            return ResponseVo.error().toString();
        }
        ResponseVo result = newsPraiseService.addPraise(userId, newsId);
        logger.info("新闻点赞>>>>{}",result.toString());
        return result.toString();
    }

    @RequestMapping(value="/newspraise",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String deletenewspraise(@RequestBody JSONObject jsonObject){
        logger.info("取消点赞>>>>{}",jsonObject.toJSONString());
        String userId = jsonObject.getString("userId");
        String newsId = jsonObject.getString("newsId");
        if (Strings.isNullOrEmpty(userId) || Strings.isNullOrEmpty(newsId)) {
            return ResponseVo.error().toString();
        }
        ResponseVo result = newsPraiseService.cancelPraise(userId, newsId);
        logger.info("取消点赞>>>>{}",result.toString());
        return result.toString();
    }

}
